package cn.com.surker.sale.service.impl;

import cn.com.surker.base.strength.dao.BaseStrengthLevelDao;
import cn.com.surker.base.strength.entity.BaseStrengthLevel;
import cn.com.surker.common.Code;
import cn.com.surker.enums.sale.AdjustPriceTypeEnum;
import cn.com.surker.enums.sale.BasicPriceTypeEnum;
import cn.com.surker.enums.sale.ContractPriceTypeEnum;
import cn.com.surker.enums.sale.SpecialPriceTypeEnum;
import cn.com.surker.enums.system.PriceTaxEnum;
import cn.com.surker.production.entity.ProductionIndent;
import cn.com.surker.sale.dao.*;
import cn.com.surker.sale.entity.*;
import cn.com.surker.sale.service.SaleContractInfoPriceBaseService;
import cn.com.surker.sale.util.PriceInfoUtil;
import cn.com.surker.spring.RestResultDto;
import cn.com.surker.system.entity.SysUser;
import cn.com.surker.util.DateUtil;
import cn.com.surker.util.ReportDateUtils;
import cn.com.surker.util.StringUtils;
import cn.com.surker.vo.sale.SaleContractInfoPriceVO;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.*;

/**
* <p>
    * 销售合同信息价 服务实现类
    * </p>
*
* @author admin
* @since 2024-07-28
*/
public abstract class SaleContractInfoPriceBaseServiceImpl extends ServiceImpl<SaleContractInfoPriceDao, SaleContractInfoPrice> implements SaleContractInfoPriceBaseService {

    @Resource
    protected SaleContractInfoPriceDao contractInfoPriceDao;
    @Resource
    protected SaleContractDao saleContractDao;
    @Resource
    protected SaleStrengthSpecialPriceDao strengthSpecialPriceDao;
    @Resource
    protected SaleContractStrengthPriceDao contractStrengthPriceDao;
    @Resource
    protected SaleStrengthAdjustPriceDao adjustPriceDao;
    @Resource
    protected BaseStrengthLevelDao baseStrengthLevelDao;

    public static void main(String[] args){
        String today = ReportDateUtils.getMonthEndTime(DateUtil.convertDate("2023-03-09 16:05:59.443"), false);
        System.out.println(today);
        System.out.println("C30细石水下".substring(0,3));

        System.out.println("LC5.0".substring(0,5));
        Double price = 235.0;
        Double contractRate = StringUtils.divide(100 + 3.0, 100.0).doubleValue();
        price = StringUtils.formatDouble(StringUtils.multiplyDouble(price, contractRate), 2);
        System.out.println(price + "   " + contractRate);
    }
    /**
     * 计算信息价
     * @param orgId
     * @param conId
     * @param today
     * @return
     */
    public List<SaleContractStrengthPrice> calcInfoPrice(Long orgId, Long conId, String today, String takeTime,
                                                         Integer roundType, Double addPrice) throws Exception {
        if (StringUtils.isAllParamNull(orgId, today))
            throw new Exception("计算参数为空");
        if (StringUtils.isNull(roundType))
            roundType = 0;//默认四舍五入
        //查询信息价地区sql
        String infoSql = "select bill_area from sale_contract where org_id = "+orgId+" and price_type = " + ContractPriceTypeEnum.XXJ.getCode()
                + " and status = 0 ";
        QueryWrapper<SaleContract> contractQuery = new QueryWrapper<>();
        contractQuery.eq("status", 0);
        contractQuery.eq("price_type", ContractPriceTypeEnum.XXJ.getCode());
        contractQuery.eq("org_id", orgId);
        if (!StringUtils.isNull(conId) && conId.intValue() > 0) {
            infoSql += " and id = "+ conId;
            contractQuery.eq("id", conId);
        }
        contractQuery.orderByAsc("adjust_price_type");
        List<SaleContract> contractList = saleContractDao.selectList(contractQuery);//查询合同信息
        if (contractList == null || contractList.size() == 0)
            throw new Exception("未获取到计算合同信息");
        QueryWrapper<SaleContractInfoPrice> infoQuery = new QueryWrapper<>();
        infoSql += "  GROUP BY bill_area ";
        infoQuery.eq("status", 0);
        infoQuery.eq("today", today);
        infoQuery.inSql("area", infoSql);
        infoQuery.and(orderWrapper -> orderWrapper.gt("price", 0).or()
                .gt("tax_price", 0));
        List<SaleContractInfoPrice> infoList = contractInfoPriceDao.selectList(infoQuery);
        if (infoList == null || infoList.size() == 0)
            throw new Exception("未获取到信息价列表");
        Map<String, List<SaleContractInfoPrice>> infoMap = convertAreaInfo(infoList);
        List<SaleContractStrengthPrice> priceList = new ArrayList<>();
        for (int i = 0; i < contractList.size(); i++) {
            SaleContract contract = contractList.get(i);
            if (StringUtils.isNull(contract.getBillArea()))
                throw new Exception("未获取到" + contract.getName() + "结算地区结算价");
            List<SaleContractInfoPrice> infoPriceList_ = infoMap.get(contract.getBillArea());
            if (infoPriceList_ == null || infoPriceList_.size() == 0)
                throw new Exception("未获取到" + contract.getName() + "信息价列表");
            QueryWrapper<SaleStrengthAdjustPrice> adjustPriceQuery = new QueryWrapper<>();
            adjustPriceQuery.eq("status", 0);
            adjustPriceQuery.eq("contract_id", contract.getId());
            adjustPriceQuery.eq("type", contract.getAdjustPriceType());
            List<SaleStrengthAdjustPrice> adjustPriceList = adjustPriceDao.selectList(adjustPriceQuery);
            if (contract.getAdjustPriceType().intValue() == AdjustPriceTypeEnum.ZW.getCode()) {
                adjustPriceList = new ArrayList<>();
            } else {
                if (adjustPriceList == null || adjustPriceList.size() == 0)
                    throw new Exception("未获取到" + contract.getName() + "信息调整基础信息");
            }

            Map<String, SaleContractInfoPrice> infoPriceMap = convertStrength(infoPriceList_);
            List<SaleContractStrengthPrice> priceList_ = calcStrength(contract, infoPriceMap, adjustPriceList, today, takeTime, roundType, addPrice);
            priceList.addAll(priceList_);
        }
        return priceList;
    }

    /**
     * 计算信息价
     * @param contract 合同
     * @param infoPriceMap 信息价
     * @param adjustPriceList 信息价调整配置
     * @param today
     * @return
     */
    private List<SaleContractStrengthPrice> calcStrength(SaleContract contract, Map<String, SaleContractInfoPrice> infoPriceMap,
                                 List<SaleStrengthAdjustPrice> adjustPriceList, String today, String contractTakeTime, Integer roundType,Double addPrice) throws Exception {
        //查询信息价关联特性
        QueryWrapper<SaleStrengthSpecialPrice> specialQuery = new QueryWrapper<>();
        specialQuery.eq("status", 0);
        specialQuery.eq("contract_id", contract.getId());
        specialQuery.inSql("contract_id", "select id from sale_contract where org_id = " + contract.getOrgId() +
                " and price_type = " + ContractPriceTypeEnum.XXJ.getCode() + " and status = 0");
        List<SaleStrengthSpecialPrice> specialList = strengthSpecialPriceDao.selectList(specialQuery);
        String takeTime_ = today + "-25 08:30:00";
        if (!StringUtils.isNull(contractTakeTime)) {
            takeTime_ = contractTakeTime;
        } else {
            takeTime_ = DateUtil.convertString(DateUtil.convertBeforeMonthDate(DateUtil.convertDate(takeTime_), 1));
        }
        List<Map<String, Object>> strengthPriceList = contractStrengthPriceDao.findContractStrengthV1(contract.getOrgId(), contract.getId(), takeTime_,"asc");
        List<Map<String, Object>> contractStrengthPriceList = new ArrayList<>();//合同价
        Map<String, SaleContractInfoPrice> contrastPriceMap = new HashMap<>();//比对价
        //如果是固定价差或对比率,查询合同价 以基础标号价差
        if (contract.getAdjustPriceType().intValue() == AdjustPriceTypeEnum.JC.getCode().intValue()
                || contract.getAdjustPriceType().intValue() == AdjustPriceTypeEnum.GDJC.getCode().intValue()
                || contract.getAdjustPriceType().intValue() == AdjustPriceTypeEnum.ZDGDJC.getCode().intValue()
                || contract.getAdjustPriceType().intValue() == AdjustPriceTypeEnum.DYBH.getCode().intValue()
                || contract.getAdjustPriceType().intValue() == AdjustPriceTypeEnum.JCJE.getCode().intValue()
                || contract.getAdjustPriceType().intValue() == AdjustPriceTypeEnum.ZDJC.getCode().intValue()
                || contract.getAdjustPriceType().intValue() == AdjustPriceTypeEnum.ZDGDHTJC.getCode().intValue()
                || contract.getAdjustPriceType().intValue() == AdjustPriceTypeEnum.ZDJCJE.getCode().intValue()) {
            contractStrengthPriceList = contractStrengthPriceDao.findContractStrength(contract.getOrgId(), contract.getId(), null,"asc");
            if (contractStrengthPriceList == null || contractStrengthPriceList.size() == 0)
                throw new Exception("未获取到" + contract.getName() + "价差对比价");

            SaleStrengthAdjustPrice adjustPrice = adjustPriceList.get(0);
            String queryToday = "";
            if (adjustPrice.getBasisPriceType().intValue() == BasicPriceTypeEnum.ZW.getCode())
                throw new Exception(contract.getName() + " 只能按合同签订月、指定月类型生成信息价");
            if (adjustPrice.getBasisPriceType().intValue() == BasicPriceTypeEnum.QDY.getCode()) {
                queryToday = ReportDateUtils.getMonthEndTime(DateUtil.convertDate(contract.getSignDate().substring(0,10), "yyyy-MM-dd"), false).substring(0,7);
            } else if (adjustPrice.getBasisPriceType().intValue() == BasicPriceTypeEnum.GDY.getCode()) {
                queryToday = adjustPrice.getPriceDay();
            }
            if (StringUtils.isNull(queryToday))//指定比对价日期为空
                throw new Exception(contract.getName() + "价差对比日期为空");
            QueryWrapper<SaleContractInfoPrice> infoQuery = new QueryWrapper<>();
            infoQuery.eq("status", 0);
            infoQuery.eq("today", queryToday);
            infoQuery.eq("area", contract.getBillArea());
            infoQuery.and(orderWrapper -> orderWrapper.gt("price", 0).or()
                    .gt("tax_price", 0));
            List<SaleContractInfoPrice> infoList = contractInfoPriceDao.selectList(infoQuery);
            if (infoList == null || infoList.size() == 0)//比对价为空
                throw new Exception(contract.getName() + "价差对比信息价列表为空");
            contrastPriceMap = convertStrength(infoList);
        }
        if (contract.getAdjustPriceType().intValue() == AdjustPriceTypeEnum.ZDJCBH.getCode().intValue()
            || contract.getAdjustPriceType().intValue() == AdjustPriceTypeEnum.ZDGDHTXF.getCode().intValue()) {
            contractStrengthPriceList = contractStrengthPriceDao.findContractStrength(contract.getOrgId(), contract.getId(), null,"asc");
            if (contractStrengthPriceList == null || contractStrengthPriceList.size() == 0)
                throw new Exception("未获取到" + contract.getName() + "价差对比合同价");
        }
        Integer takeFlag = 0;
        if (strengthPriceList == null || strengthPriceList.size() == 0)
            throw new Exception(contract.getName() + "基础标号列表为空");

        Date nowDate = new Date();
        String takeTime = "";
        if (strengthPriceList != null && strengthPriceList.size() > 0) {
            takeTime = StringUtils.convertString(strengthPriceList.get(0).get("takeTime"));
            Date takeDate = DateUtil.convertDate(takeTime);
            Date endDate = DateUtil.convertDate(today + "-25 08:30:00");
            Date startDate = DateUtil.convertBeforeMonthDate(endDate, 1);
            if (takeDate.getTime() >= startDate.getTime() && takeDate.getTime() < endDate.getTime()) {
                takeFlag = 1;//如果价格存在
            } else {//如果价格不存在
                takeTime = today + takeTime.substring(7);
            }
        }
        Map<String, SaleStrengthAdjustPrice> adjustPriceMap = convertAdjust(adjustPriceList);
        Map<String, Map<String, Object>> basicMap = calcBasicPrice(strengthPriceList, infoPriceMap, adjustPriceMap,
                contractStrengthPriceList, contrastPriceMap);
        if (basicMap == null || basicMap.keySet().size() == 0)
            throw new Exception(contract.getName() + "基础价格计算列表为空");

        Map<String, SaleStrengthSpecialPrice> specialPriceMap = convertSpecial(specialList);
        List<SaleContractStrengthPrice> priceList = new ArrayList<>();
        Integer adjustPriceType = contract.getAdjustPriceType();//信息价调整类型
        for (String key : basicMap.keySet()) {
            String baseStrength = "";//基础标号
            String special = "";//特性
            if (key.indexOf("C") == 0) {
                if (key.length() > 3) {//特性标号
                    if (key.indexOf("C") == 0) {
                        baseStrength = key.substring(0,3);
                        special = key.substring(3);
                    }
                } else {
                    baseStrength = key;
                }
            } else if (key.indexOf("LC") == 0) {
                if (key.length() > 5) {//特性标号
                    if (key.indexOf(".") != -1) {
                        baseStrength = key.substring(0,5);
                        special = key.substring(5);
                    } else {
                        baseStrength = key.substring(0,4);
                        special = key.substring(4);
                    }
                } else {
                    baseStrength = key;
                }
            } else if (key.indexOf("M") == 0) {
                if (key.indexOf(".") != -1) {
                    baseStrength = key.substring(0,4);
                    special = key.substring(4);
                } else {
                    baseStrength = key.substring(0,3);
                    special = key.substring(3);
                }
            }

            Long id = StringUtils.convertLong(basicMap.get(key).get("id"));
            Long strengthId = StringUtils.convertLong(basicMap.get(key).get("strengthId"));
            if (StringUtils.isNull(basicMap.get(key).get("id")) || id == 0)
                continue;
            if (key.indexOf("LC") == 0) {
                SaleContractStrengthPrice strengthPrice = new SaleContractStrengthPrice(contract.getOrgId(), contract.getId(),
                        strengthId, 1.0, DateUtil.convertDate(takeTime), 0, "系统计算信息价", "", nowDate);
                if (takeFlag == 1)
                    strengthPrice.setId(id);
                priceList.add(strengthPrice);
                continue;
            }

            SaleStrengthAdjustPrice strengthAdjustPrice = adjustPriceMap.get(baseStrength);
            Integer calcPriceType = 0;
            if (strengthAdjustPrice != null && !StringUtils.isNull(strengthAdjustPrice.getCalcAdjustType())) {
                calcPriceType = strengthAdjustPrice.getCalcAdjustType();
            }
            Double adjustPrice = StringUtils.convertDouble(basicMap.get(key).get("adjustPrice"));//调整价格
            Double adjustTaxPrice = StringUtils.convertDouble(basicMap.get(key).get("adjustTaxPrice"));//调整含税价
            Double rate = StringUtils.convertDouble(basicMap.get(key).get("rate"));//调整率
            Double contrastPrice = StringUtils.convertDouble(basicMap.get(key).get("contrastPrice"));//比对价
            Double contrastTaxPrice = StringUtils.convertDouble(basicMap.get(key).get("contrastTaxPrice"));//比对含税价
            Double infoPrice = StringUtils.convertDouble(basicMap.get(key).get("infoPrice"));//信息价
            Double infoTaxPrice = StringUtils.convertDouble(basicMap.get(key).get("infoTaxPrice"));//信息含税价
            Double contractPrice = StringUtils.convertDouble(basicMap.get(key).get("contractPrice"));//合同价
            if (!StringUtils.isNull(special)) {//如果是特性标号
                if (!basicMap.containsKey(baseStrength))
                    continue;
                adjustPrice = StringUtils.convertDouble(basicMap.get(baseStrength).get("adjustPrice"));//调整价格
                adjustTaxPrice = StringUtils.convertDouble(basicMap.get(baseStrength).get("adjustTaxPrice"));//调整含税价
                rate = StringUtils.convertDouble(basicMap.get(baseStrength).get("rate"));//调整率
                contrastPrice = StringUtils.convertDouble(basicMap.get(baseStrength).get("contrastPrice"));//比对价
                contrastTaxPrice = StringUtils.convertDouble(basicMap.get(baseStrength).get("contrastTaxPrice"));//比对含税价
                infoPrice = StringUtils.convertDouble(basicMap.get(baseStrength).get("infoPrice"));//信息价
                infoTaxPrice = StringUtils.convertDouble(basicMap.get(baseStrength).get("infoTaxPrice"));//信息含税价
                contractPrice = StringUtils.convertDouble(basicMap.get(baseStrength).get("contractPrice"));//合同价
            }
            Double price = 0.0;//执行价
            Double specialPrice = 0.0;
            if (!StringUtils.isNull(baseStrength) && !StringUtils.isNull(special)) {
                if(!infoPriceMap.containsKey(baseStrength))
                    continue;
                infoPrice = StringUtils.convertDouble(infoPriceMap.get(baseStrength).getPrice());
                infoTaxPrice = StringUtils.convertDouble(infoPriceMap.get(baseStrength).getTaxPrice());
                if (contrastPriceMap != null && contrastPriceMap.keySet().size() > 0 && contrastPriceMap.containsKey(baseStrength)) {
                    contrastPrice = StringUtils.convertDouble(contrastPriceMap.get(baseStrength).getPrice());
                    contrastTaxPrice = StringUtils.convertDouble(contrastPriceMap.get(baseStrength).getTaxPrice());
                }
                for (String specialKey : specialPriceMap.keySet()) {
                    if (special.indexOf(specialKey) != -1) {//如果是固定价
                        SaleStrengthSpecialPrice strengthSpecialPrice = specialPriceMap.get(specialKey);
                        if (strengthSpecialPrice.getType().intValue() == SpecialPriceTypeEnum.GD.getCode().intValue()) {
                            specialPrice += strengthSpecialPrice.getPrice();
                        } else {//如果是提级价
                            BaseStrengthLevel strengthLevel = baseStrengthLevelDao.findStrengthDetail(baseStrength, null, null);
                            if (strengthLevel == null)
                                continue;
                            //基础价格信息
                            Double adjustPrice1 = StringUtils.convertDouble(basicMap.get(strengthLevel.getLevel()).get("adjustPrice"));//调整价格
                            Double adjustTaxPrice1 = StringUtils.convertDouble(basicMap.get(strengthLevel.getLevel()).get("adjustTaxPrice"));//调整含税价
                            Double rate1 = StringUtils.convertDouble(basicMap.get(strengthLevel.getLevel()).get("rate"));//调整率
                            Double contrastPrice1 = StringUtils.convertDouble(basicMap.get(strengthLevel.getLevel()).get("contrastPrice"));//比对价
                            Double contrastTaxPrice1 = StringUtils.convertDouble(basicMap.get(strengthLevel.getLevel()).get("contrastTaxPrice"));//比对含税价
                            Double infoPrice1 = StringUtils.convertDouble(basicMap.get(strengthLevel.getLevel()).get("infoPrice"));//信息价
                            Double infoTaxPrice1 = StringUtils.convertDouble(basicMap.get(strengthLevel.getLevel()).get("infoTaxPrice"));//信息含税价
                            Double contractPrice1 = StringUtils.convertDouble(basicMap.get(strengthLevel.getLevel()).get("contractPrice"));//合同价
                            Double level1 = calcLevelPrice(contract,infoPrice1, adjustPrice1, contrastPrice1,
                                    infoTaxPrice1, adjustTaxPrice1, contrastTaxPrice1, infoPriceMap, adjustPriceList,
                                    contrastPriceMap, contractPrice1, rate1, basicMap, calcPriceType, roundType);
                            Integer sort = strengthLevel.getSort() + strengthSpecialPrice.getLevel();
                            BaseStrengthLevel strengthLevel1 = baseStrengthLevelDao.findStrengthDetail(null, strengthLevel.getType(), sort);
                            if (strengthLevel1 == null) {
                                specialPrice += 0.0;
                            } else {//提级标号价格信息
                                Double adjustPrice2 = StringUtils.convertDouble(basicMap.get(strengthLevel1.getLevel()).get("adjustPrice"));//调整价格
                                Double adjustTaxPrice2 = StringUtils.convertDouble(basicMap.get(strengthLevel1.getLevel()).get("adjustTaxPrice"));//调整含税价
                                Double rate2 = StringUtils.convertDouble(basicMap.get(strengthLevel1.getLevel()).get("rate"));//调整率
                                Double contrastPrice2 = StringUtils.convertDouble(basicMap.get(strengthLevel1.getLevel()).get("contrastPrice"));//比对价
                                Double contrastTaxPrice2 = StringUtils.convertDouble(basicMap.get(strengthLevel1.getLevel()).get("contrastTaxPrice"));//比对含税价
                                Double infoPrice2 = StringUtils.convertDouble(basicMap.get(strengthLevel1.getLevel()).get("infoPrice"));//信息价
                                Double infoTaxPrice2 = StringUtils.convertDouble(basicMap.get(strengthLevel1.getLevel()).get("infoTaxPrice"));//信息含税价
                                Double contractPrice2 = StringUtils.convertDouble(basicMap.get(strengthLevel1.getLevel()).get("contractPrice"));//合同价
                                Double level2 = calcLevelPrice(contract,infoPrice2, adjustPrice2, contrastPrice2,
                                        infoTaxPrice2, adjustTaxPrice2, contrastTaxPrice2, infoPriceMap, adjustPriceList,
                                        contrastPriceMap, contractPrice2, rate2, basicMap, calcPriceType, roundType);
                                Double tempAdjustPrice = StringUtils.convertDouble(level2 - level1);
                                if (tempAdjustPrice < 0)//如果计算出来的价差错误,提级价变更为0
                                    tempAdjustPrice = 0.0;
                                specialPrice += tempAdjustPrice;
                            }
                        }
                    }
                }
            }
            Double tempInfoPrice = infoPrice;
            Double tempAdjustPrice = adjustPrice;
            Double tempContrastPrice = contrastPrice;
            if (contract.getIsPriceTax().intValue() == PriceTaxEnum.YES.getCode().intValue()) {//如果合同含税
                tempInfoPrice = infoTaxPrice;
                tempAdjustPrice = adjustTaxPrice;
                tempContrastPrice = contrastTaxPrice;
            }
            if (adjustPriceType.intValue() == AdjustPriceTypeEnum.DYBH.getCode().intValue()) {//以C30为基准
                strengthAdjustPrice = adjustPriceList.get(0);
                infoPrice = StringUtils.convertDouble(infoPriceMap.get(strengthAdjustPrice.getStrength()).getPrice());
                infoTaxPrice = StringUtils.convertDouble(infoPriceMap.get(strengthAdjustPrice.getStrength()).getTaxPrice());
                //基准月份价格
                Double tempStrengthInfoPrice = StringUtils.convertDouble(contrastPriceMap.get(strengthAdjustPrice.getStrength()).getPrice());
                Double tempStrengthInfoTaxPrice = StringUtils.convertDouble(contrastPriceMap.get(strengthAdjustPrice.getStrength()).getTaxPrice());
                adjustPrice = StringUtils.subtractDouble(infoPrice, tempStrengthInfoPrice);
                adjustTaxPrice = StringUtils.subtractDouble(infoTaxPrice, tempStrengthInfoTaxPrice);
                if (contract.getIsPriceTax().intValue() == PriceTaxEnum.NO.getCode().intValue())
                    price = PriceInfoUtil.calcJCBH(contractPrice, adjustPrice, roundType);
                else
                    price = PriceInfoUtil.calcJCBH(contractPrice, adjustTaxPrice, roundType);
            } else if (adjustPriceType.intValue() == AdjustPriceTypeEnum.JCBH.getCode().intValue()) {//以基础标号为准
                price = PriceInfoUtil.calcJCBH(tempInfoPrice, tempAdjustPrice, roundType);
            } else if (adjustPriceType.intValue() == AdjustPriceTypeEnum.GDJC.getCode().intValue()) {//按合同价基础标号调整
                price = PriceInfoUtil.calcGDJC(tempInfoPrice, contractPrice, tempContrastPrice, roundType);
            } else if (adjustPriceType.intValue() == AdjustPriceTypeEnum.ZDGDJC.getCode().intValue()) {//按合同价指定标号调整(元)
                strengthAdjustPrice = adjustPriceList.get(0);
                String strength_ = strengthAdjustPrice.getStrength();
                adjustPrice = StringUtils.convertDouble(strengthAdjustPrice.getPrice());
                tempInfoPrice = StringUtils.convertDouble(infoPriceMap.get(strength_).getPrice());
                tempContrastPrice = StringUtils.convertDouble(basicMap.get(strength_).get("contractPrice"));//合同价
                if (contract.getIsPriceTax().intValue() == PriceTaxEnum.YES.getCode().intValue()) {
                    adjustPrice = StringUtils.convertDouble(strengthAdjustPrice.getTaxPrice());
                    tempInfoPrice = StringUtils.convertDouble(infoPriceMap.get(strength_).getTaxPrice());
                    tempContrastPrice = StringUtils.convertDouble(basicMap.get(strength_).get("contrastTaxPrice"));//比对含税价
//                    tempContrastPrice = StringUtils.convertDouble(strengthAdjustPrice.getTaxPrice());
                }
                price = PriceInfoUtil.calcJCJE(tempInfoPrice, contractPrice, tempContrastPrice, adjustPrice, roundType);
            } else if (adjustPriceType.intValue() == AdjustPriceTypeEnum.XF.getCode().intValue()) {//下浮率
                price = PriceInfoUtil.calcXF(tempInfoPrice, rate, roundType);
            } else if (adjustPriceType.intValue() == AdjustPriceTypeEnum.JC.getCode().intValue()) {//价差比
                price = PriceInfoUtil.calcJC(tempInfoPrice, contractPrice, tempContrastPrice, rate,
                        calcPriceType, roundType);
            } else if (adjustPriceType.intValue() == AdjustPriceTypeEnum.ZDGDHTXF.getCode().intValue()) {//按指定标号调整固定价差(元)
                strengthAdjustPrice = adjustPriceList.get(0);
                String strength_ = strengthAdjustPrice.getStrength();
                Double zdTempContractPrice = StringUtils.convertDouble(basicMap.get(strength_).get("contractPrice"));
                tempInfoPrice = StringUtils.convertDouble(infoPriceMap.get(strength_).getPrice());
//                tempAdjustPrice = StringUtils.convertDouble(strengthAdjustPrice.getPrice());
                //指定标号基准信息价
                if (contract.getIsPriceTax().intValue() == PriceTaxEnum.YES.getCode().intValue()) {
                    tempInfoPrice = StringUtils.convertDouble(infoPriceMap.get(strength_).getTaxPrice());
                }
                price = PriceInfoUtil.calcZDHTXF(zdTempContractPrice, contractPrice, tempInfoPrice,
                        strengthAdjustPrice.getRate(), roundType);
            } else if (adjustPriceType.intValue() == AdjustPriceTypeEnum.JCJE.getCode().intValue()) {
                price = PriceInfoUtil.calcJCJE(tempInfoPrice, contractPrice, tempContrastPrice, tempAdjustPrice, roundType);
            } else if (adjustPriceType.intValue() == AdjustPriceTypeEnum.ZW.getCode().intValue()) {//无类型
                price = tempInfoPrice;
            } else if (adjustPriceType.intValue() == AdjustPriceTypeEnum.ZDJC.getCode().intValue()) {//按指定标号调整价差对比率(%)
                strengthAdjustPrice = adjustPriceList.get(0);
                rate = strengthAdjustPrice.getRate();
                price = PriceInfoUtil.calcJC(tempInfoPrice, contractPrice, tempContrastPrice, rate,
                        strengthAdjustPrice.getCalcAdjustType(), roundType);
            } else if (adjustPriceType.intValue() == AdjustPriceTypeEnum.ZDJCJE.getCode().intValue()) {//按指定标号调整价差对比金额(元)
                strengthAdjustPrice = adjustPriceList.get(0);
                tempAdjustPrice = StringUtils.convertDouble(infoPriceMap.get(strengthAdjustPrice.getStrength()).getPrice());
                String strength_ = strengthAdjustPrice.getStrength();
                tempContrastPrice = StringUtils.convertDouble(basicMap.get(strength_).get("contrastPrice"));//比对价
                if (contract.getIsPriceTax().intValue() == PriceTaxEnum.NO.getCode().intValue()) {
                    tempAdjustPrice = StringUtils.convertDouble(infoPriceMap.get(strengthAdjustPrice.getStrength()).getTaxPrice());
                } else if (contract.getIsPriceTax().intValue() == PriceTaxEnum.YES.getCode().intValue()) {
                    tempContrastPrice = StringUtils.convertDouble(basicMap.get(baseStrength).get("contrastTaxPrice"));//比对含税价
                }
                price = PriceInfoUtil.calcJCJE(tempInfoPrice, contractPrice, tempContrastPrice, tempAdjustPrice, roundType);
            } else if (adjustPriceType.intValue() == AdjustPriceTypeEnum.ZDJCBH.getCode().intValue()) {//按指定标号调整固定价差(元)
                strengthAdjustPrice = adjustPriceList.get(0);
                String strength_ = strengthAdjustPrice.getStrength();
                Double zdTempContractPrice = StringUtils.convertDouble(basicMap.get(strength_).get("contractPrice"));
                tempInfoPrice = StringUtils.convertDouble(infoPriceMap.get(strength_).getPrice());
                tempAdjustPrice = StringUtils.convertDouble(strengthAdjustPrice.getPrice());
                if (contract.getIsPriceTax().intValue() == PriceTaxEnum.YES.getCode().intValue()) {
                    tempAdjustPrice = StringUtils.convertDouble(strengthAdjustPrice.getTaxPrice());
                    tempInfoPrice = StringUtils.convertDouble(infoPriceMap.get(strength_).getTaxPrice());
                }
                price = PriceInfoUtil.calcZDJCBH(zdTempContractPrice, contractPrice, tempInfoPrice, tempAdjustPrice, roundType);
            } else if (adjustPriceType.intValue() == AdjustPriceTypeEnum.ZDGDHTJC.getCode().intValue()) {//按指定标号调整固定价差(元)
                strengthAdjustPrice = adjustPriceList.get(0);
                String strength_ = strengthAdjustPrice.getStrength();
                Double zdTempContractPrice = StringUtils.convertDouble(basicMap.get(strength_).get("contractPrice"));
                tempInfoPrice = StringUtils.convertDouble(infoPriceMap.get(strength_).getPrice());
//                tempAdjustPrice = StringUtils.convertDouble(strengthAdjustPrice.getPrice());
                //指定标号基准信息价
                Double tempStrengthInfoPrice = StringUtils.convertDouble(contrastPriceMap.get(strength_).getPrice());
                tempAdjustPrice = StringUtils.subtractDouble(tempStrengthInfoPrice, tempInfoPrice);
                if (contract.getIsPriceTax().intValue() == PriceTaxEnum.YES.getCode().intValue()) {
                    tempStrengthInfoPrice = StringUtils.convertDouble(contrastPriceMap.get(strength_).getTaxPrice());
                    tempInfoPrice = StringUtils.convertDouble(infoPriceMap.get(strength_).getTaxPrice());
                    tempAdjustPrice = StringUtils.subtractDouble(tempStrengthInfoPrice, tempInfoPrice);
                }
                price = PriceInfoUtil.calcZDGDHTJC(zdTempContractPrice, contractPrice, tempAdjustPrice, roundType);
            }
            if (baseStrength.equals("C25")) {
                System.out.println("2222");
            }
            if (contract.getIsPriceTax().intValue() == PriceTaxEnum.NO.getCode().intValue()) {//未含税单价加上开票税率
                Double contractRate = StringUtils.divide(100 + contract.getRate(), 100.0).doubleValue();
//                price = StringUtils.formatDouble(price, 2, roundType); //5月22号取消计算不含税率
                price = StringUtils.multiplyDouble(price, contractRate);
            }
            if (!StringUtils.isNull(addPrice)) {//最后累加金额
                price += addPrice;
            }

            price = StringUtils.formatDouble(StringUtils.addDouble(price, specialPrice), 2, roundType);

            if (price < 1)
                price = 1.0;
            if (key.indexOf("M") == 0 || key.indexOf("LC") == 0) {
                price = contractPrice;
            }
            SaleContractStrengthPrice strengthPrice = new SaleContractStrengthPrice(contract.getOrgId(), contract.getId(), strengthId, price,
                DateUtil.convertDate(takeTime), 0, "系统计算信息价", "", nowDate);
//            if (takeFlag == 1)
//                strengthPrice.setId(id);
            priceList.add(strengthPrice);
        }
        return priceList;
    }

    /**
     * 计算标号单价
     * @param contract 合同
     * @param infoPrice 信息价
     * @param adjustPrice 调整价
     * @param contrastPrice 对比价
     * @param infoTaxPrice 含税信息价
     * @param adjustTaxPrice 调整含税价
     * @param contrastTaxPrice 对比含税价
     * @param infoPriceMap 信息价map
     * @param adjustPriceList 调整价集合
     * @param contrastPriceMap 对比价集合
     * @param contractPrice 合同价
     * @param rate 调整率
     * @return
     */
    private Double calcLevelPrice(SaleContract contract,Double infoPrice, Double adjustPrice, Double contrastPrice,
                                  Double infoTaxPrice, Double adjustTaxPrice, Double contrastTaxPrice, Map<String, SaleContractInfoPrice> infoPriceMap,
                                  List<SaleStrengthAdjustPrice> adjustPriceList, Map<String, SaleContractInfoPrice> contrastPriceMap,
                                  Double contractPrice, Double rate,Map<String, Map<String, Object>> basicMap, Integer calcPriceType,Integer roundType) {
        Double price = 0.0;
        Double tempInfoPrice = infoPrice;
        Double tempAdjustPrice = adjustPrice;
        Double tempContrastPrice = contrastPrice;
        if (contract.getIsPriceTax().intValue() == PriceTaxEnum.YES.getCode().intValue()) {
            tempInfoPrice = infoTaxPrice;
            tempAdjustPrice = adjustTaxPrice;
            tempContrastPrice = contrastTaxPrice;
        }
        if (contract.getAdjustPriceType().intValue() == AdjustPriceTypeEnum.DYBH.getCode().intValue()) {//以C30为基准
            SaleStrengthAdjustPrice strengthAdjustPrice = adjustPriceList.get(0);
            infoPrice = StringUtils.convertDouble(infoPriceMap.get(strengthAdjustPrice.getStrength()).getPrice());
            infoTaxPrice = StringUtils.convertDouble(infoPriceMap.get(strengthAdjustPrice.getStrength()).getTaxPrice());
            //基准月份价格
            Double tempStrengthInfoPrice = StringUtils.convertDouble(contrastPriceMap.get(strengthAdjustPrice.getStrength()).getPrice());
            Double tempStrengthInfoTaxPrice = StringUtils.convertDouble(contrastPriceMap.get(strengthAdjustPrice.getStrength()).getTaxPrice());
            adjustPrice = StringUtils.subtractDouble(infoPrice, tempStrengthInfoPrice);
            adjustTaxPrice = StringUtils.subtractDouble(infoTaxPrice, tempStrengthInfoTaxPrice);
            if (contract.getIsPriceTax().intValue() == PriceTaxEnum.NO.getCode().intValue())
                price = PriceInfoUtil.calcJCBH(contractPrice, adjustPrice, roundType);
            else
                price = PriceInfoUtil.calcJCBH(contractPrice, adjustTaxPrice, roundType);
        } else if (contract.getAdjustPriceType().intValue() == AdjustPriceTypeEnum.JCBH.getCode().intValue()) {//以基础标号为准
            price = PriceInfoUtil.calcJCBH(tempInfoPrice, tempAdjustPrice, roundType);
        } else if (contract.getAdjustPriceType().intValue() == AdjustPriceTypeEnum.GDJC.getCode().intValue()) {//固定价差
            price = PriceInfoUtil.calcGDJC(tempInfoPrice, contractPrice, tempContrastPrice, roundType);
        } else if (contract.getAdjustPriceType().intValue() == AdjustPriceTypeEnum.ZDGDHTXF.getCode().intValue()) {//按指定标号调整固定价差(元)
            SaleStrengthAdjustPrice strengthAdjustPrice = adjustPriceList.get(0);
            String strength_ = strengthAdjustPrice.getStrength();
            Double zdTempContractPrice = StringUtils.convertDouble(basicMap.get(strength_).get("contractPrice"));
            tempInfoPrice = StringUtils.convertDouble(infoPriceMap.get(strength_).getPrice());
//                tempAdjustPrice = StringUtils.convertDouble(strengthAdjustPrice.getPrice());
            //指定标号基准信息价
            if (contract.getIsPriceTax().intValue() == PriceTaxEnum.YES.getCode().intValue()) {
                tempInfoPrice = StringUtils.convertDouble(infoPriceMap.get(strength_).getTaxPrice());
            }
            price = PriceInfoUtil.calcZDHTXF(zdTempContractPrice, contractPrice, tempInfoPrice,
                    rate, roundType);
        }  else if (contract.getAdjustPriceType().intValue() == AdjustPriceTypeEnum.XF.getCode().intValue()) {//下浮率
            price = PriceInfoUtil.calcXF(tempInfoPrice, rate, roundType);
        } else if (contract.getAdjustPriceType().intValue() == AdjustPriceTypeEnum.ZDGDHTXF.getCode().intValue()) {//按合同价指定标号调整(元)
            SaleStrengthAdjustPrice strengthAdjustPrice = adjustPriceList.get(0);
            String strength_ = strengthAdjustPrice.getStrength();
            adjustPrice = StringUtils.convertDouble(strengthAdjustPrice.getPrice());
            tempInfoPrice = StringUtils.convertDouble(infoPriceMap.get(strength_).getPrice());
            tempContrastPrice = StringUtils.convertDouble(basicMap.get(strength_).get("contractPrice"));//合同价
            if (contract.getIsPriceTax().intValue() == PriceTaxEnum.YES.getCode().intValue()) {
                adjustPrice = StringUtils.convertDouble(strengthAdjustPrice.getTaxPrice());
                tempInfoPrice = StringUtils.convertDouble(infoPriceMap.get(strength_).getTaxPrice());
                tempContrastPrice = StringUtils.convertDouble(basicMap.get(strength_).get("contrastTaxPrice"));//比对含税价
//                    tempContrastPrice = StringUtils.convertDouble(strengthAdjustPrice.getTaxPrice());
            }
            price = PriceInfoUtil.calcJCJE(tempInfoPrice, contractPrice, tempContrastPrice, rate, roundType);
        } else if (contract.getAdjustPriceType().intValue() == AdjustPriceTypeEnum.JC.getCode().intValue()) {//价差比
            price = PriceInfoUtil.calcJC(tempInfoPrice, contractPrice, tempContrastPrice, rate, calcPriceType, roundType);
        }  else if (contract.getAdjustPriceType().intValue() == AdjustPriceTypeEnum.JCJE.getCode().intValue()) {//价差比
            price = PriceInfoUtil.calcJCJE(tempInfoPrice, contractPrice, tempContrastPrice, tempAdjustPrice, roundType);
        } else if (contract.getAdjustPriceType().intValue() == AdjustPriceTypeEnum.ZW.getCode().intValue()) {
            price = tempInfoPrice;
        } else if (contract.getAdjustPriceType().intValue() == AdjustPriceTypeEnum.ZDJC.getCode().intValue()) {
            SaleStrengthAdjustPrice strengthAdjustPrice = adjustPriceList.get(0);
            rate = strengthAdjustPrice.getRate();
            price = PriceInfoUtil.calcJC(tempInfoPrice, contractPrice, tempContrastPrice, rate,
                    strengthAdjustPrice.getCalcAdjustType(), roundType);
        } else if (contract.getAdjustPriceType().intValue() == AdjustPriceTypeEnum.ZDJCJE.getCode().intValue()) {//按指定标号调整价差对比金额(元)
            SaleStrengthAdjustPrice strengthAdjustPrice = adjustPriceList.get(0);
            adjustPrice = StringUtils.convertDouble(infoPriceMap.get(strengthAdjustPrice.getStrength()).getPrice());
            String strength_ = strengthAdjustPrice.getStrength();
            tempInfoPrice = StringUtils.convertDouble(infoPriceMap.get(strength_).getPrice());
            tempContrastPrice = StringUtils.convertDouble(basicMap.get(strength_).get("contractPrice"));//合同价
            if (contract.getIsPriceTax().intValue() == PriceTaxEnum.YES.getCode().intValue()) {
                adjustPrice = StringUtils.convertDouble(strengthAdjustPrice.getTaxPrice());
                tempInfoPrice = StringUtils.convertDouble(infoPriceMap.get(strength_).getTaxPrice());
                tempContrastPrice = StringUtils.convertDouble(basicMap.get(strength_).get("contrastTaxPrice"));//比对含税价
//                    tempContrastPrice = StringUtils.convertDouble(strengthAdjustPrice.getTaxPrice());
            }
            price = PriceInfoUtil.calcJCJE(tempInfoPrice, contractPrice, tempContrastPrice, adjustPrice, roundType);
        } else if (contract.getAdjustPriceType().intValue() == AdjustPriceTypeEnum.ZDJCBH.getCode().intValue()) {//按指定标号调整固定价差(元)
            SaleStrengthAdjustPrice strengthAdjustPrice = adjustPriceList.get(0);
            String strength_ = strengthAdjustPrice.getStrength();
            Double zdTempContractPrice = StringUtils.convertDouble(basicMap.get(strength_).get("contractPrice"));
            tempInfoPrice = StringUtils.convertDouble(infoPriceMap.get(strength_).getPrice());
            tempAdjustPrice = StringUtils.convertDouble(strengthAdjustPrice.getPrice());
            if (contract.getIsPriceTax().intValue() == PriceTaxEnum.YES.getCode().intValue()) {
                tempAdjustPrice = StringUtils.convertDouble(strengthAdjustPrice.getTaxPrice());
                tempInfoPrice = StringUtils.convertDouble(infoPriceMap.get(strength_).getTaxPrice());
                zdTempContractPrice = StringUtils.convertDouble(basicMap.get(strength_).get("contractTaxPrice"));
            }
            price = PriceInfoUtil.calcZDJCBH(zdTempContractPrice, contractPrice, tempInfoPrice, tempAdjustPrice, roundType);

        } else if (contract.getAdjustPriceType().intValue() == AdjustPriceTypeEnum.ZDGDHTJC.getCode().intValue()) {//按指定标号调整固定价差(元)
            SaleStrengthAdjustPrice strengthAdjustPrice = adjustPriceList.get(0);
            String strength_ = strengthAdjustPrice.getStrength();
            Double zdTempContractPrice = StringUtils.convertDouble(basicMap.get(strength_).get("contractPrice"));
            tempInfoPrice = StringUtils.convertDouble(infoPriceMap.get(strength_).getPrice());
//                tempAdjustPrice = StringUtils.convertDouble(strengthAdjustPrice.getPrice());
            //指定标号基准信息价
            Double tempStrengthInfoPrice = StringUtils.convertDouble(contrastPriceMap.get(strength_).getPrice());
            tempAdjustPrice = StringUtils.subtractDouble(tempStrengthInfoPrice, tempInfoPrice);
            if (contract.getIsPriceTax().intValue() == PriceTaxEnum.YES.getCode().intValue()) {
                tempStrengthInfoPrice = StringUtils.convertDouble(contrastPriceMap.get(strength_).getTaxPrice());
                tempInfoPrice = StringUtils.convertDouble(infoPriceMap.get(strength_).getTaxPrice());
                tempAdjustPrice = StringUtils.subtractDouble(tempStrengthInfoPrice, tempInfoPrice);
            }
            price = PriceInfoUtil.calcZDGDHTJC(zdTempContractPrice, contractPrice, tempAdjustPrice, roundType);
        }
        if (contract.getIsPriceTax().intValue() == PriceTaxEnum.NO.getCode().intValue()) {//未含税单价加上开票税率
            Double contractRate = StringUtils.divide(100 + contract.getRate(), 100.0).doubleValue();
            price = StringUtils.formatDouble(StringUtils.multiplyDouble(price, contractRate), 2);
        }
        return price;
    }

    /**
     *
     * @param strengthPriceList 原始砼单价
     * @param infoPriceMap  结算月信息价
     * @param adjustPriceMap 调整属性
     * @param contractStrengthPriceList 合同价
     * @param contrastPriceMap 信息比对价
     * @return
     */
    private Map<String, Map<String, Object>> calcBasicPrice(List<Map<String, Object>> strengthPriceList,
       Map<String, SaleContractInfoPrice> infoPriceMap, Map<String, SaleStrengthAdjustPrice> adjustPriceMap,
       List<Map<String, Object>> contractStrengthPriceList, Map<String, SaleContractInfoPrice> contrastPriceMap) {
        Map<String, Map<String, Object>> strengthMap = new HashMap<>();
        for (int i = 0; i < strengthPriceList.size(); i++) {//将已有价格存入
            Map<String, Object> temp = strengthPriceList.get(i);
            String strength = StringUtils.convertString(temp.get("strength"));
            String strength_ = "";
            Double adjustPrice = 0.0;//调整价格
            Double adjustTaxPrice = 0.0;//调整含税价
            Double rate = 0.0;//调整率
            Double contrastPrice = 0.0;//比对价
            Double contrastTaxPrice = 0.0;//比对含税价
            Double infoPrice = 0.0;//信息价
            Double infoTaxPrice = 0.0;//信息含税价
            if (strength.indexOf("C") == 0) {
                if (strength.length() > 3) {//特性标号
                    if (strength.indexOf("C") == 0) {
                        strength_ = strength.substring(0,3);
                    } else {
                        strength_ = strength;
                    }
                } else {
                    strength_ = strength;
                }
            } else if (strength.indexOf("LC") == 0) {
                if (strength.length() > 5) {//特性标号
                    if (strength.indexOf(".") != -1) {
                        strength_ = strength.substring(0,5);
                    } else {
                        strength_ = strength.substring(0,4);
                    }
                } else {
                    strength_ = strength;
                }
            } else if (strength.indexOf("M") == 0) {
                if (strength.indexOf(".") != -1) {
                    strength_ = strength.substring(0,4);
                } else {
                    strength_ = strength.substring(0,3);
                }
            }
            if (adjustPriceMap.containsKey(strength_)) {
                adjustPrice = adjustPriceMap.get(strength_).getPrice();
                adjustTaxPrice = adjustPriceMap.get(strength_).getTaxPrice();
                rate = adjustPriceMap.get(strength_).getRate();
            }
            if (infoPriceMap.containsKey(strength_)) {
                infoPrice = infoPriceMap.get(strength_).getPrice();
                infoTaxPrice = infoPriceMap.get(strength_).getTaxPrice();
            }
            if (contrastPriceMap != null && contrastPriceMap.keySet().size() > 0 && contrastPriceMap.containsKey(strength_)) {
                contrastPrice = contrastPriceMap.get(strength_).getPrice();
                contrastTaxPrice = contrastPriceMap.get(strength_).getTaxPrice();
            }
            Map<String, Object> resultMap = new HashMap<>();
            resultMap.put("id", temp.get("id"));//标号ID
            resultMap.put("strengthId", temp.get("strengthId"));//标号ID
            resultMap.put("price", temp.get("price"));//最后一次单价信息
            resultMap.put("adjustPrice", adjustPrice);//调整价格
            resultMap.put("adjustTaxPrice", adjustTaxPrice);//调整含税价
            resultMap.put("rate", rate);//调整率
            resultMap.put("contrastPrice", contrastPrice);//比对价
            resultMap.put("contrastTaxPrice", contrastTaxPrice);//比对含税价
            resultMap.put("infoPrice", infoPrice);//信息价
            resultMap.put("infoTaxPrice", infoTaxPrice);//信息价

            strengthMap.put(strength, resultMap);
        }
        for (String key : adjustPriceMap.keySet()) {
            if (strengthMap.containsKey(key))
                continue;
            Double adjustPrice = 0.0;//调整价格
            Double adjustTaxPrice = 0.0;//调整含税价
            Double rate = 0.0;//调整率
            Double contrastPrice = 0.0;//比对价
            Double contrastTaxPrice = 0.0;//比对含税价
            Double infoPrice = 0.0;//信息价
            Double infoTaxPrice = 0.0;//信息含税价
            if (adjustPriceMap.containsKey(key)) {
                adjustPrice = StringUtils.convertDouble(adjustPriceMap.get(key).getPrice());
                adjustTaxPrice = StringUtils.convertDouble(adjustPriceMap.get(key).getTaxPrice());
                rate = StringUtils.convertDouble(adjustPriceMap.get(key).getRate());
            }
            if (infoPriceMap.containsKey(key)) {
                infoPrice = StringUtils.convertDouble(infoPriceMap.get(key).getPrice());
                infoTaxPrice = StringUtils.convertDouble(infoPriceMap.get(key).getTaxPrice());
            }
            if (contrastPriceMap != null && contrastPriceMap.keySet().size() > 0 && contrastPriceMap.containsKey(key)) {
                contrastPrice = StringUtils.convertDouble(contrastPriceMap.get(key).getPrice());
                contrastTaxPrice = StringUtils.convertDouble(contrastPriceMap.get(key).getTaxPrice());
            }
            Map<String, Object> resultMap = new HashMap<>();
            resultMap.put("id", null);//标号ID
            resultMap.put("strengthId", adjustPriceMap.get(key).getStrengthId());//标号ID
            resultMap.put("price", 0.0);//最后一次单价信息
            resultMap.put("adjustPrice", adjustPrice);//调整价格
            resultMap.put("adjustTaxPrice", adjustTaxPrice);//调整含税价
            resultMap.put("rate", rate);//调整率
            resultMap.put("contrastPrice", contrastPrice);//比对价
            resultMap.put("contrastTaxPrice", contrastTaxPrice);//比对含税价
            resultMap.put("infoPrice", infoPrice);//信息价
            resultMap.put("infoTaxPrice", infoTaxPrice);//信息价

            strengthMap.put(key, resultMap);
        }
        if (contractStrengthPriceList != null && contractStrengthPriceList.size() > 0) {
            for (int i = 0; i < contractStrengthPriceList.size(); i++) {
                Map<String, Object> temp = contractStrengthPriceList.get(i);
                String strength = StringUtils.convertString(temp.get("strength"));
                if (strengthMap.containsKey(strength)) {
                    Map<String, Object> resultMap = strengthMap.get(strength);
                    resultMap.put("contractPrice", temp.get("price"));
                    strengthMap.put(strength, resultMap);
                }
            }
        }

        return strengthMap;
    }
    /**
     * 转换标号调整价
     * @param adjustPriceList 调整特性
     * @return
     */
    private Map<String, SaleStrengthAdjustPrice> convertAdjust(List<SaleStrengthAdjustPrice> adjustPriceList) {
        Map<String, SaleStrengthAdjustPrice> adjustPriceMap = new HashMap<>();
        if (adjustPriceList == null || adjustPriceList.size() == 0)
            return adjustPriceMap;

        for (int i = 0; i < adjustPriceList.size(); i++) {
            SaleStrengthAdjustPrice adjustPrice = adjustPriceList.get(i);
            String strength = adjustPrice.getStrength();
            adjustPriceMap.put(strength, adjustPrice);
        }
        return adjustPriceMap;
    }
    /**
     * 转换价格特性价
     * @param specialList 调整特性
     * @return
     */
    private Map<String, SaleStrengthSpecialPrice> convertSpecial(List<SaleStrengthSpecialPrice> specialList) {
        Map<String, SaleStrengthSpecialPrice> specialPriceMap = new HashMap<>();
        for (int i = 0; i < specialList.size(); i++) {
            SaleStrengthSpecialPrice specialPrice = specialList.get(i);
            String strength = specialPrice.getEquirement();
            specialPriceMap.put(strength, specialPrice);
        }
        return specialPriceMap;
    }


    /**
     * 转换标号信息价
     * @param infoList
     * @return
     */
    private Map<String, SaleContractInfoPrice> convertStrength(List<SaleContractInfoPrice> infoList) {
        Map<String, SaleContractInfoPrice> infoPriceMap = new HashMap<>();
        for (int i = 0; i < infoList.size(); i++) {
            SaleContractInfoPrice infoPrice = infoList.get(i);
            String strength = infoPrice.getStrength();
            infoPriceMap.put(strength, infoPrice);
        }
        return infoPriceMap;
    }

    /**
     * 信息价地区
     * @param infoList
     * @return
     */
    private Map<String, List<SaleContractInfoPrice>> convertAreaInfo(List<SaleContractInfoPrice> infoList) {
        Map<String, List<SaleContractInfoPrice>> infoPriceMap = new HashMap<>();
        for (int i = 0; i < infoList.size(); i++) {
            SaleContractInfoPrice infoPrice = infoList.get(i);
            String area = infoPrice.getArea();
            if (infoPriceMap.containsKey(area)) {
                List<SaleContractInfoPrice> list = infoPriceMap.get(area);
                list.add(infoPrice);
                infoPriceMap.put(area, list);
            } else {
                List<SaleContractInfoPrice> list = new ArrayList<>();
                list.add(infoPrice);
                infoPriceMap.put(area, list);
            }
        }
        return infoPriceMap;
    }

    @Override
    public IPage<SaleContractInfoPriceVO> queryPage(String searchVal, String startDate, Integer audit,Integer status, Integer offset, Integer limit) {
        PageHelper.offsetPage(offset, limit);
        Page<SaleContractInfoPrice> voPage = new Page<>((offset + limit) / limit, limit);
        IPage<SaleContractInfoPriceVO> resultPage = new Page<>();
        QueryWrapper<SaleContractInfoPrice> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNull(status))
            status = 0;
        /**
         * 关键字查询
         */
        if(StrUtil.isNotEmpty(searchVal)){
            queryWrapper.lambda().and(p -> p.like(SaleContractInfoPrice::getArea, searchVal).or().like(SaleContractInfoPrice::getStrength, searchVal).or().like(SaleContractInfoPrice::getPrice,
                    searchVal).or().like(SaleContractInfoPrice::getTaxPrice, searchVal).or().like(SaleContractInfoPrice::getRemark, searchVal));
        }
        if (StrUtil.isNotEmpty(startDate)){
            queryWrapper.lambda().eq(SaleContractInfoPrice::getToday,startDate);
        }
        if (ObjectUtil.isNotEmpty(audit)){
            queryWrapper.lambda().eq(SaleContractInfoPrice::getIsCheck,audit);
        }
        if(ObjectUtil.isNotEmpty(status)){
            queryWrapper.lambda().eq(SaleContractInfoPrice::getStatus,status);
        }
        queryWrapper.lambda().orderByDesc(SaleContractInfoPrice::getArea,SaleContractInfoPrice::getToday);
        IPage<SaleContractInfoPrice> iPage = contractInfoPriceDao.selectPage(voPage, queryWrapper);
        /**分页插件**/
        if (!StringUtils.isNull(iPage.getRecords())) {
            PageInfo pageInfo = new PageInfo(iPage.getRecords());
            if (!StringUtils.isNull(pageInfo.getTotal()))
                iPage.setTotal(pageInfo.getTotal());
        }
        if(ObjectUtil.isNotEmpty(iPage)){
            BeanUtil.copyProperties(iPage,resultPage);
        }
        return resultPage;
    }

    @Override
    public RestResultDto saveInit(SaleContractInfoPrice dto,SysUser user) {
        UpdateContractInfoPrice(dto.getStrength(), dto.getArea(), dto.getToday(),user);
        contractInfoPriceDao.insert(dto);
        return RestResultDto.success("保存成功！");
    }

    @Override
    public RestResultDto update(Long id,String price,String taxPrice,String remark,SysUser user) {
        SaleContractInfoPrice saleContractInfoPrice = contractInfoPriceDao.selectById(id);
        if(ObjectUtil.isEmpty(saleContractInfoPrice)){
            return RestResultDto.fail("当前数据不存在！");
        }
        if(StrUtil.isNotEmpty(price)){
            saleContractInfoPrice.setPrice(Double.valueOf(price));
        }
        if(StrUtil.isNotEmpty(taxPrice)){
            saleContractInfoPrice.setTaxPrice(Double.valueOf(taxPrice));
        }
        if(StrUtil.isNotEmpty(remark)){
            saleContractInfoPrice.setRemark(remark);
        }
        saleContractInfoPrice.setUpdator(user.getName());
        saleContractInfoPrice.setUpdateTime(new Date());
        contractInfoPriceDao.updateById(saleContractInfoPrice);
        return RestResultDto.success("更新成功！");
    }

    /**
     * 查询当期数据是否存在
     * @param strength 标号
     * @param area  地区
     * @param today 信息价时间
     * @return
     */
    public void  UpdateContractInfoPrice(String strength, String area, String today, SysUser user){

        UpdateWrapper<SaleContractInfoPrice> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().set(SaleContractInfoPrice::getStatus,1);
        updateWrapper.lambda().set(SaleContractInfoPrice::getUpdateTime,new Date());
        updateWrapper.lambda().set(SaleContractInfoPrice::getUpdator,user.getUsername());
        updateWrapper.lambda().set(SaleContractInfoPrice::getRemark,user.getName() + "覆盖新增");
        updateWrapper.lambda().eq(SaleContractInfoPrice::getStrength,strength);
        updateWrapper.lambda().eq(SaleContractInfoPrice::getArea,area);
        updateWrapper.lambda().eq(SaleContractInfoPrice::getToday,today);
        contractInfoPriceDao.update(null,updateWrapper);
    }

	@Override
	protected Class<SaleContractInfoPrice> currentModelClass() {
		return SaleContractInfoPrice.class;
	}

}
